﻿using Framework;

namespace Game
{
    public class SceneObjectDieActionable : IBTreeActionable<SceneObjectBTContext>
    {
        private bool _isDied = false;
        private SceneObjectBTContext _context;
        
        public void OnStart(SceneObjectBTContext context)
        {
            _context = context;
            context.sceneObject.GetComponent<SceneObjectActionComponent>().SetTrigger(SceneObjectActions.Die);
            Scheduler.Instance.AddTimeListener(SceneConst.DieDuration, OnDieEnd, 1);
        }

        public void OnEnd(SceneObjectBTContext context)
        {
            Scheduler.Instance.RemoveTimeListener(OnDieEnd);
        }

        public TaskStatus Update(SceneObjectBTContext context)
        {
            if (_isDied)
            {
                return TaskStatus.Success;
            }
            return TaskStatus.Running;
        }
        
        private void OnDieEnd(float deltaTime)
        {
            _isDied = true;
            (_context.sceneObject as SceneBattleEntity).Die();
        }
    }
}